<template>
  <el-form :model="state.form" :inline="true" class="my-form" label-position="right" label-width="70px">
    <el-form-item label="商家">
      <el-input clearable v-model="state.form.client_search" style="width: 240px;" placeholder="请输入商家编号、名称、简称搜索" />
    </el-form-item>
    <el-form-item label="联系人">
      <el-input clearable v-model="state.form.contacts_search" style="width: 200px;" placeholder="输入联系人姓名、电话搜索" />
    </el-form-item>
    <el-form-item label="所属国家">
      <el-select clearable v-model="state.form.belong_country" placeholder="请选择所属国家" style="width: 145px;">
        <el-option :label="val" :value="key" v-for="(val,key) in state.country" :key="key"/>
      </el-select>
    </el-form-item>
    <el-form-item label="商家状态">
      <el-select clearable v-model="state.form.client_status" placeholder="请选择商家状态" style="width: 145px;">
        <el-option :label="val" :value="key" v-for="(val,key) in state.status" :key="key"/>
      </el-select>
    </el-form-item>
    <el-form-item label="创建时间">
      <el-date-picker style="width: 280px;" v-model="state.selectData" :editable="false" start-placeholder="结束时间" end-placeholder="结束时间"
        type="daterange" range-separator="至" value-format="YYYY-MM-DD" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit" :loading="isLoading" icon="Search">搜索</el-button>
    </el-form-item>
  </el-form>
</template>

<script setup>
import { reactive, onMounted, watchEffect } from 'vue'
import { merchantConfigInit } from '@/api/merchantManage/merchantManage';
import { useCommonStore } from '@/stores';
import { isEmpty } from 'lodash-es';

const commonStore = useCommonStore();
const props = defineProps({
  isLoading: {
    type: Boolean,
    default: false
  }
})

const emit = defineEmits(['search'])

const state = reactive({
  form: {
    client_search: '',
    contacts_search: '',
    belong_country: '',
    start_time: '',
    end_time: '',
    client_status: ''
  },
  status: [],
  country:[],
  selectData: ''
})


watchEffect(() => {
  if (state.selectData) {
    state.form.start_time = state.selectData[0]
    state.form.end_time = state.selectData[1]
  }else{
    state.form.start_time = ''
    state.form.end_time = ''
  }
})

// methods
const onSubmit = () => { 
  emit('search', state.form)
}


defineExpose({
  state
})

// life
onMounted(async () => {
  if(!isEmpty(commonStore.merchantInitConfig)){
    state.country = commonStore.merchantInitConfig.country
    state.status = commonStore.merchantInitConfig.status
    return;
  }
  const res = await merchantConfigInit()
    if (res.code === 1000) {
      state.country = res.data.country
      state.status = res.data.status||[]
      commonStore.merchantInitConfig = res.data||{};
    }
 
})
</script>

<style lang="less" scoped>
.my-form {
  padding: var(--el-main-padding);
  font-size: 16px;

  .text-center {
    text-align: center;
  }

  :deep(.el-form-item) {
    margin-right: 25px;
  }
}
</style>